package org.shanktech.tshare.binlogsync.process;

import java.util.Map;

import org.shanktech.tshare.binlogsync.facotry.TableParserFactory;
import org.shanktech.tshare.binlogsync.model.binlog.BinlogData;
import org.shanktech.tshare.binlogsync.util.Constant;

@SuppressWarnings("unchecked")
public abstract class AbstractTableTransfer implements TableTransfer {

  
  @Override
  public void transfer(BinlogData binlogData) {
    Object object = binlogData.getBinlogdataParser().parser(binlogData,
        TableParserFactory.getInstance(binlogData.getTableName()));

    // 注意：Object可能是null,需要对null进行处理
    if (Constant.SqlDML.INSERT.getValue()
        .equalsIgnoreCase(binlogData.getType())) {
      insert(binlogData, object);
    } else if (Constant.SqlDML.UPDATE.getValue()
        .equalsIgnoreCase(binlogData.getType())) {
      Map<String, Object> data = (Map<String, Object>)object;
      update(binlogData, data.get("afterObject"), data.get("beforObject"));
    } else if (Constant.SqlDML.DELETE.getValue()
        .equalsIgnoreCase(binlogData.getType())) {
      delete(binlogData, object);
    }
  }

  protected abstract void insert(BinlogData binlogData, Object object);

  protected abstract void update(BinlogData binlogData, Object afterObject, Object beforObject);

  protected abstract void delete(BinlogData binlogData, Object object);

}
